package pl.edu.agh.io.fx3.treecreator.disk;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import pl.edu.agh.io.fx3.exceptions.ConfigFileException;
import pl.edu.agh.io.fx3.filesystem.FileSystemFactory;
import pl.edu.agh.io.fx3.utils.JDBCInfo;

public class DiskConfigFileParser {

	public void parseConfFile(File configFile, DiskTreeConfig diskTreeConfig)
			throws ConfigFileException {
		try {
			BufferedReader in = new BufferedReader(new FileReader(configFile));

			String strLine;
			while ((strLine = in.readLine()) != null) {
				if (strLine.compareTo(DiskTreeConfig.JDBC) == 0) {
					JDBCInfo jdbcInfo = diskTreeConfig.getJdbcInfo();
					for (int i = 0; i < DiskTreeConfig.JDBC_OPTION_NUMBER; ++i) {
						strLine = in.readLine();
						if (strLine == null) {
							break;
						}

						String[] values = strLine.split("=");
						if (values == null || values.length != 2) {
							throw new ConfigFileException();
						}

						if (values[0].compareTo(DiskTreeConfig.DRIVER) == 0) {
							jdbcInfo.setDriver(values[1]);
						} else if (values[0].compareTo(DiskTreeConfig.URL) == 0) {
							jdbcInfo.setUrl(values[1]);
						} else if (values[0].compareTo(DiskTreeConfig.USERNAME) == 0) {
							jdbcInfo.setUsername(values[1]);
						} else if (values[0].compareTo(DiskTreeConfig.PASSWORD) == 0) {
							jdbcInfo.setPassword(values[1]);
						} else {
							throw new ConfigFileException();
						}
					}
				} else if (strLine.compareTo(DiskTreeConfig.HIERARCHY) == 0) {
					for (int i = 0; i < DiskTreeConfig.HIERARCHY_OPTION_NUMBER; ++i) {
						strLine = in.readLine();
						if (strLine == null) {
							break;
						}

						String[] values = strLine.split("=");
						if (values == null || values.length != 2) {
							throw new ConfigFileException();
						}

						if (values[0].compareTo(DiskTreeConfig.MAX_FILES) == 0) {
							int tmp = Integer.parseInt(values[1]);
							if (tmp >= 0) {
								diskTreeConfig.setMaxFiles(tmp);
							}
						} else if (values[0]
								.compareTo(DiskTreeConfig.MAX_FOLDERS) == 0) {
							int tmp = Integer.parseInt(values[1]);
							if (tmp >= 0) {
								diskTreeConfig.setMaxFolders(tmp);
							}
						} else if (values[0]
								.compareTo(DiskTreeConfig.ROOT_PATH) == 0) {
							diskTreeConfig
									.setRootPath(stripLeadingAndTrailingQuotes(values[1]));
						} else {
							throw new ConfigFileException();
						}
					}
				}
			}
		} catch (FileNotFoundException e) {
			FileSystemFactory.LOGGER.error(e);
		} catch (IOException e) {
			FileSystemFactory.LOGGER.error(e);
		}
		FileSystemFactory.LOGGER.info("Config file " + configFile + " parsed.");
	}

	private String stripLeadingAndTrailingQuotes(String str) {
		if (str.startsWith("\"")) {
			str = str.substring(1, str.length());
		}
		if (str.endsWith("\"")) {
			str = str.substring(0, str.length() - 1);
		}
		return str;
	}
	
	public static JDBCInfo getJDBCInfo(InputStream input) throws ConfigFileException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(input));

		String driver = null, url = null, username = null, password = null;
		String strLine;
		while ((strLine = in.readLine()) != null) {
			if (strLine.compareTo(DiskTreeConfig.JDBC) == 0) {
				for (int i = 0; i < DiskTreeConfig.JDBC_OPTION_NUMBER; ++i) {
					strLine = in.readLine();
					if (strLine == null) {
						break;
					}

					String[] values = strLine.split("=");
					if (values == null || values.length != 2) {
						throw new ConfigFileException();
					}

					if (values[0].compareTo(DiskTreeConfig.DRIVER) == 0) {
						driver = new String(values[1]);
					} else if (values[0].compareTo(DiskTreeConfig.URL) == 0) {
						url = new String(values[1]);
					} else if (values[0].compareTo(DiskTreeConfig.USERNAME) == 0) {
						username = new String(values[1]);
					} else if (values[0].compareTo(DiskTreeConfig.PASSWORD) == 0) {
						password = new String(values[1]);
					} else {
						throw new ConfigFileException();
					}
				}
			}
		}
		FileSystemFactory.LOGGER.info("Returning new JDBCInfo instance.");
		return new JDBCInfo(driver, url, username, password);
		
	}
}
